﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BinarTtree.IsSymmetric
{
    public class TreeNode
    {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null)
        {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    class MainProject
    {
        static void Main(string[] args)
        {
            //TreeNode treeNode = new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3, new TreeNode(6), new TreeNode(7)));
            TreeNode treeNode = new TreeNode(1, new TreeNode(2,new TreeNode(3,new TreeNode(5),new TreeNode(6)),new TreeNode(4,new TreeNode(6),new TreeNode(5))),new TreeNode(2,new TreeNode(4,new TreeNode(5),new TreeNode(6)),new TreeNode(3,new TreeNode(6),new TreeNode(5))));
            Console.WriteLine(IsSymmetric(treeNode));
            Console.ReadKey();
        }
        public static bool IsSymmetric(TreeNode root)
        {
            if (root==null)
                return true;
            return Symmetric(root.left, root.right);
        }

        public static bool Symmetric(TreeNode left, TreeNode right)
        {
            //如果左右子节点都为空，说明当前节点是叶子节点，返回true
            if (left == null && right == null)
                return true;
            //如果当前节点只有一个子节点或者有两个子节点，但两个子节点的值不相同，直接返回false
            if (left == null || right == null || left.val != right.val)
                return false;
            //然后左子节点的左子节点和右子节点的右子节点比较，左子节点的右子节点和右子节点的左子节点比较
            return Symmetric(left.left, right.right) && Symmetric(left.right, right.left);
        }
    }
}
